package com.linkhub.oms.admin.common.aspect;

import com.linkhub.oms.admin.common.exception.OrderExceptionFactory;
import com.linkhub.oms.admin.common.exception.ShopExceptionFactory;
import com.linkhub.oms.admin.modules.order.domain.param.BaseMultiOrderParam;
import com.linkhub.oms.admin.modules.order.domain.param.BaseSingleOrderParam;
import com.linkhub.oms.admin.modules.satoken.session.LhSessionContext;
import com.linkhub.oms.admin.modules.shop.domain.param.BaseShopParam;
import com.linkhub.oms.common.enums.mp.RoleCodeEnum;
import com.linkhub.oms.common.util.QtStreamUtil;
import com.linkhub.oms.dao.mpservice.IOrderService;
import lombok.extern.slf4j.Slf4j;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Pointcut;
import org.springframework.core.annotation.Order;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.List;

/**
 * @Description: 客户数据权限校验，防止数据越权
 * @Author: echo
 * @Date: 2024/4/10 16:52
 * @Version: 1.0
 */
@Aspect
@Component
@Slf4j
@Order(2)
public class CustomerDataAuthorityAspect {

    @Resource
    private IOrderService orderService;


    /**
     * 以 controller 包下定义的所有请求为切入点
     */
    @Pointcut("execution(public * com.linkhub.oms.admin.modules.*.controller.*.*(..))")
    public void webAop() {
        // empty
    }

    @Around("webAop()")
    public Object doAround(ProceedingJoinPoint proceedingJoinPoint) throws Throwable {
        // 开始打印请求日志
        Object[] args = proceedingJoinPoint.getArgs();
        for (Object arg : args) {

            if (arg instanceof BaseShopParam) {
                BaseShopParam baseShopParam = (BaseShopParam) arg;
                if (!LhSessionContext.checkLoginUserShop(baseShopParam.getSysShopId())) {
                    throw ShopExceptionFactory.getSysShopIdIllegality();
                }
            }

            if (arg instanceof BaseSingleOrderParam) {
                BaseSingleOrderParam singleOrderParam = (BaseSingleOrderParam) arg;
                if (!orderService.checkOrderExist(singleOrderParam.getSysShopId(), singleOrderParam.getSysOrderId())) {
                    throw OrderExceptionFactory.getInvalidOrderId();
                }
            }

            if (arg instanceof BaseMultiOrderParam) {
                BaseMultiOrderParam baseMultiOrderParam = (BaseMultiOrderParam) arg;
                List<String> sysShopIds = QtStreamUtil.mapAndDistinct(baseMultiOrderParam.getShopOrders(), BaseSingleOrderParam::getSysShopId);
                List<String> userRoles = LhSessionContext.getUserRoles();
                boolean employeeRole = userRoles.contains(RoleCodeEnum.EMPLOYEE.getCode());
                if (!LhSessionContext.checkLoginUserShops(sysShopIds) && !employeeRole) {
                    throw ShopExceptionFactory.getSysShopIdIllegality();
                }
            }
        }

        //请求结果
        return proceedingJoinPoint.proceed();
    }

}
